package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.FlowReTaskNoticeChannelVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReTaskNoticeChannelRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowReTaskNoticeChannel;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowReTaskNoticeChannelExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowReTaskNoticeChannelDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-07-11 16:25
 */
@Service
public class FlowReTaskNoticeChannelRepositoryImpl implements FlowReTaskNoticeChannelRepository {

    @Autowired
    private FlowReTaskNoticeChannelDAO flowReTaskNoticeChannelDAO;

    @Override
    public int insertFlowReTaskNoticeChannel(FlowReTaskNoticeChannelVo entity) {
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        return flowReTaskNoticeChannelDAO.insertSelective(convert(entity));
    }

    @Override
    public List<FlowReTaskNoticeChannelVo> listFlowReTaskNoticeChannel(Long reTimeoutSettingId) {
        FlowReTaskNoticeChannelExample query = new FlowReTaskNoticeChannelExample();
        query.createCriteria().andReTimeoutSettingIdEqualTo(reTimeoutSettingId);
        return listFlowReTaskNoticeChannel(query);
    }

    public List<FlowReTaskNoticeChannelVo> listFlowReTaskNoticeChannel(FlowReTaskNoticeChannelExample query) {
        return flowReTaskNoticeChannelDAO.selectByExample(query).stream().map(this::convert).collect(Collectors.toList());
    }

    private FlowReTaskNoticeChannel convert(FlowReTaskNoticeChannelVo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowReTaskNoticeChannel result = new FlowReTaskNoticeChannel();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FlowReTaskNoticeChannelVo convert(FlowReTaskNoticeChannel entity) {
        if (Objects.isNull(entity)) {
            return null;
        }

        FlowReTaskNoticeChannelVo result = new FlowReTaskNoticeChannelVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
